package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书");
        System.out.println("请输入你要删除的书的名字：");
        String name = scanner.nextLine();
        int size = bookList.getUseSide();
        int index = -1;
        for (int i = 0; i < size; i++){
            Book book = bookList.getBook(i);
            if (name.equals(book.getName())){
                index = i;
            }
        }
        if (index == -1){
            System.out.println("没有要删除的书");
        }
        else {
            for (int j = index; j < size - 1; j++){
                Book book = bookList.getBook(j + 1);
                bookList.setBook(j, book);
            }
            bookList.setUseSide(size - 1);
            System.out.println("删除成功");
        }
    }
}
